//
//  HTCFKeychainUitil.m
//  HTHappySale
//
//  Created by 创维海通 on 2017/9/18.
//  Copyright © 2017年 zxyywy. All rights reserved.
//

#import "HTCFKeychainUitil.h"
#import "AES128Util.h"
#import "NSData+ASE128.h"
#import "GTMBase64.h"

@implementation HTCFKeychainUitil

+ (NSString *)AES128EncryptUtil:(NSString *)plainText key:(NSString *)key{
//    return [AES128Util AES128Encrypt:plainText key:key];

    char buff_str[1024];
    memset(buff_str, 0, 1024);
    memcpy(buff_str,[plainText UTF8String], [plainText length]);
    NSLog(@"buff_str%s",buff_str);
    
    unsigned long inLen = strlen(buff_str);
    
    if (inLen % 8 != 0) {
        int temp = inLen % 8;
        inLen += 8 - temp;
    }
    
    NSLog(@"%@",key);
    char key_str[16];
    memset(key_str, 0, 16);
    int keyLen = (int)key.length;
    if (keyLen < 16) {
        int keyStart = 16 - keyLen;
        memcpy(key_str + keyStart,[key UTF8String], keyLen);
    }else{
        memcpy(key_str,[key UTF8String], keyLen);
    }
   
    NSLog(@"key:%s",key_str);
    
    char buffout_str[2048];
    memset(buffout_str, 0, 2048);
    
    int outlen = idea_encrypt_buffer(buff_str,inLen,buffout_str,key_str);

    
//    NSString *temp = @"1234567890sdfghjikoxctygijkocdfghj";
//    NSData *data = [temp dataUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes: &(buffout_str) length:outlen];
    NSString *base64String = [GTMBase64 encodeBase64Data:data];

    NSLog(@"Base:%@",base64String);
    
    return base64String;
}

+ (NSString *)AES128DecryptUtil:(NSString *)encryStr key:(NSString *)key{
    return [AES128Util AES128Decrypt:encryStr key:key];
}



@end
